package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Result;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import com.itheima.vo.SysDictDataVo;
import com.itheima.vo.SysDictTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sysDictData")
public class SysDicDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 添加字典数据
     * @param sysDictDataVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody SysDictDataVo sysDictDataVo){
        try {
            sysDictDataService.add(sysDictDataVo);
            return new Result("success","添加成功");
        } catch (Exception e) {
            return new Result("fail","添加失败");
        }
    }

    /**
     * 批量删除
     * @param sysDictDataVoList
     * @return
     */
    @PostMapping("/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody List<SysDictDataVo> sysDictDataVoList){
        try {
            sysDictDataService.deleteBatch(sysDictDataVoList);
            return new Result("success","批量删除成功");
        } catch (Exception e) {
            return new Result("fail","删除失败");
        }
    }

    /**
     * 修改数据
     * @param sysDictDataVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody SysDictDataVo sysDictDataVo){
        try {
            sysDictDataService.edit(sysDictDataVo);
            return new Result("success","修改成功");
        } catch (Exception e) {
            return new Result("fail","修改失败");
        }
    }

    /**
     * 获取字典数据列表
     * @param sysDictDataVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result list(@RequestBody(required = false) SysDictDataVo sysDictDataVo){
        PageInfo pageInfo=sysDictDataService.list(sysDictDataVo);
        return new Result("success",pageInfo);
    }

    /**
     * 根据字典类型获取字典数据
     * @param dicType
     * @return
     */
    @PostMapping("/getListByDicType")
    @ResponseBody
    public Result listBDT(@RequestParam String dicType){
      PageInfo pageInfo=  sysDictDataService.listBDT(dicType);
      return new Result("success",pageInfo);
    }

    @PostMapping("/getListByDictTypeList")
    @ResponseBody
    public Result listBDTL(@RequestBody List<String> dicTypeList){
       PageInfo pageInfo= sysDictDataService.listBDTL(dicTypeList);
       return new Result("success",pageInfo);
    }
 }
